from django.http import JsonResponse
from .models import RequestRecord
from user.views import auth
import time
from Online_Response_System.config.base import host
from chat.views import make_vaild_time

# 国家 省份 城市
@auth(True)
def stat_ip(request):
    if request.method == 'GET':
        country = request.GET.get('country')
        province = request.GET.get('province')
        city = request.GET.get('city')

        start_time =make_vaild_time(float(request.GET.get('start_time')),0)
        end_time =make_vaild_time(float(request.GET.get('end_time')),time.time())
        data, data_path = RequestRecord.stat_ip(country, province, city, start_time, end_time)

        return JsonResponse(
            {
                'code': '200',
                'msg': '响应成功!',
                'data': {
                    'data_link': f'{host}{data_path}',
                    'data': data
                }
            }
        )


# 浏览器统计
@auth(True)
def stat_browser(request):
    if request.method == 'GET':
        start_time =make_vaild_time(float(request.GET.get('start_time')),0)
        end_time =make_vaild_time(float(request.GET.get('end_time')),time.time())

        data, data_path = RequestRecord.stat_browser(start_time, end_time)
        return JsonResponse(
            {
                'code': '200',
                'msg': '响应成功!',
                'data': {
                    'data_link': f'{host}{data_path}',
                    'data': data
                }
            }
        )
